<template>
  <div class="data">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-card class="box-card mt-15">
          <div class="home-form">
            <el-form ref="ruleForm" label-width="80px" @submit.native.prevent>
              <el-row :gutter="10">
                <el-col :span="6">
                  <el-form-item label="购买日期" prop="dateData">
                    <el-date-picker
                      style="width: 100%"
                      size="mini"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      v-model="ruleForm.dateData"
                      type="datetimerange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="3">
                  <el-form-item label="课程类型" prop="courseType">
                    <el-select v-model="ruleForm.courseType" placeholder="请选择" style="width:100%" clearable size="mini">
                      <el-option
                        v-for="item in courseTypeArr"
                        :key="item.dicCode"
                        :label="item.dicName"
                        :value="item.dicCode">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="签约状态" prop="courseState">
                    <el-select v-model="ruleForm.courseState" multiple collapse-tags  placeholder="请选择" style="width:100%" clearable size="mini">
                      <el-option
                        v-for="item in signCourseStateArr"
                        :key="item.dicCode"
                        :label="item.dicName"
                        :value="item.dicCode">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="3">
                  <el-form-item label="关联教练" prop="teachCoachUserId">
                    <el-select v-model="ruleForm.teachCoachUserId" filterable placeholder="请选择" style="width:100%" clearable size="mini">
                      <el-option
                        v-for="item in coachArr"
                        :key="item.id"
                        :label="item.userName"
                        :value="item.id">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="会员" prop="key">
                    <el-input  placeholder="请输入会员名称或手机号" size="mini" v-model.trim="ruleForm.key" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch" class="search-btn">搜索</el-button>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-card>
        <el-card class="box-card">
          <!-- 头部 -->
          <div slot="header" class="clearfix fix-lh">
            <span>课程签约</span>
            <div class="btn-list">
              <el-button size="mini" @click="handleAdd" v-hasPermission="permission.add">新增</el-button>
            </div>
          </div>
          <div class="home-body">
            <cvue-table
              :option="tableOption"
              :data="tableData"
              ref="tbl1"
              :isShowAdd="false"
              :page="page"
              :tablePage="tablePage"
              :pageSize="page.pageSize"
              :loading="tableLoading"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              @select-change="handleSelectChange"
              :isShowPage="true">
              <template slot-scope="scope" slot="courseType">
                <span>{{ scope.row.courseType === 1 ?'私教课' : '自由练'}}</span>
              </template>
              <template slot-scope="scope" slot="payMoney">
                <span>{{ (scope.row.payMoney/100).toFixed(2) }}</span>
              </template>
              <template slot-scope="scope" slot="signCourseState">
                <span>{{ getSignCourseState(scope.row.signCourseState) }}</span>
              </template>
              <template slot-scope="scope" slot="remainCount">
                <span v-if="scope.row.remainCount">{{ scope.row.remainCount + '/' + scope.row.buyCount }}</span>
                <span v-else>-</span>
              </template>
              <!-- 操作按钮 -->
              <el-table-column fixed="right" slot="actionMenu" label="操作" align="center" header-align="center" width="160">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="handleDelete(scope.row)" v-hasPermission="permission.edit" v-if="scope.row.signCourseState === 0 || scope.row.signCourseState === 1">删除</el-button>
                  <el-button type="text" size="small" @click="handleEdit(scope.row)" v-hasPermission="permission.edit">修改</el-button>
                  <el-button type="text" size="small" @click="handleView(scope.row)" v-hasPermission="permission.view">查看</el-button>
                  <el-button type="text" size="small" @click="handleComfirm(scope.row)" v-if="scope.row.signCourseState === 0" v-hasPermission="permission.edit">确认支付</el-button>
                </template>
              </el-table-column>
            </cvue-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
/* eslint-disable */
import cvueTable from '@/components/cvue-table'
export default {
  name: 'course-data',
  components: {
    cvueTable
  },
  data () {
    return {
      tablePage: 1,
      tableLoading: false,
      page: {
        total: 1, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
        pageSizes: [10, 50, 100]
      },
      tableOption: {
        stripe: true,
        width: '100%',
        border: true,
        index: false,
        selected: false,
        cloumn: [
          { prop: 'index', label: '', align: 'center', width: 58 },
          { prop: 'userName', label: '姓名', align: 'center', sortable: true },
          { prop: 'phone', label: '手机号', align: 'center', sortable: true },
          { prop: 'courseName', label: '课程名称', align: 'center', sortable: true },
          { prop: 'courseType', label: '课程类型', align: 'center', sortable: true, slotStatus: true },
          { prop: 'signCourseState', label: '签约状态', align: 'center', sortable: true, slotStatus: true },
          { prop: 'payTime', label: '购买时间', align: 'center', sortable: true },
          { prop: 'teachCoachUserName', label: '关联教练', align: 'left', sortable: true },
          { prop: 'expirationEndTime', label: '有效期至', align: 'center', sortable: true },
          { prop: 'payMoney', label: '支付金额', align: 'center', sortable: true, slotStatus: true },
          { prop: 'payType', label: '购买方式', align: 'center', sortable: true },
          { prop: 'remainCount', label: '剩余课时', align: 'center', slotStatus: true },
          // { prop: 'remainCount1', label: '合约残值', align: 'center' },
          { prop: 'updateTime', label: '更新时间', align: 'center', sortable: true },
          { prop: 'updateUserName', label: '操作人', align: 'center', sortable: true }
        ]
      },
      tableData: [
        // { name: 'ddd', code: 'sss', order: 1, status: 1 }
      ],
      dialogIndexEdit: false,
      actionType: 'add',
      rowData: {},
      courseModeArr: [],
      courseStateArr: [],
      courseTypeArr: [],
      saleModeArr: [],
      coachArr: [],
      ruleForm: {
        dateData: [],
        courseType: '',
        courseState: [],
        teachCoachUserIdList: '',
        key: ''
      },
      selectForm: {
        dateData: [],
        courseType: '',
        courseState: [],
        teachCoachUserIdList: '',
        key: ''
      },
      signCourseStateArr: []
    }
  },
  created () {
    // console.log(this.$route)
    this.getDic().then(res => {
      // console.log(res)
      this.dict = res.data
      this.dict.forEach((item, index) => {
        if (item.dicName == 'CourseMode') {
          this.courseModeArr = item.dicValueList
        }
        if (item.dicName == 'CourseState') {
          this.courseStateArr = item.dicValueList
        }
        if (item.dicName == 'CourseType') {
          this.courseTypeArr = item.dicValueList
        }
        if (item.dicName == 'CourseSaleMode') {
          this.saleModeArr = item.dicValueList
        }
        if (item.dicName == 'CourseState') {
          this.courseStateArr = item.dicValueList
        }
        if (item.dicName == 'SignCourseState') {
          this.signCourseStateArr = item.dicValueList
        }
      })
    })
    this.handleList(
      this.selectForm.dateData[0],
      this.selectForm.dateData[1],
      this.selectForm.courseType,
      this.selectForm.courseState,
      this.selectForm.teachCoachUserId,
      this.selectForm.key
    )
    this.getCoachList()
  },
  activated () {
    this.handleList(
      this.selectForm.dateData[0],
      this.selectForm.dateData[1],
      this.selectForm.courseType,
      this.selectForm.courseState,
      this.selectForm.teachCoachUserId,
      this.selectForm.key
    )
  },
  methods: {
    getSignCourseState (val) {
      if (val === 0) {
        return '未支付'
      } else if(val === 1) {
        return '未开始'
      } else if(val === 2) {
        return '正常'
      } else if(val === -1) {
        return '过期'
      } else if(val === 3) {
        return '暂停'
      } else {
        return ''
      }
    },
    getCoachList () {
      var params = {
        current: 1,
        size: 1000,
        key: ''
      }
      this.$store.dispatch('manage/CoachVisibleList', params).then(res => {
        // console.log(res)
        if (res.code == '200') {
          var data = res.data
          this.coachArr = data
        }
      }).catch(err => {
        console.log(err)
      })
    },
    handleResetForm () {
      this.$refs['ruleForm'].resetFields()
    },
    // 新增
    handleAdd () {
      this.actionType = 'add'
      var item = {
        value: '/home/course-data-edit'
      }
      this.$store.commit('common/CLOSE_TAG', item)
      this.$nextTick(() => {
        this.$router.push({path: '/home/course-data-edit', query: {action: 'add'}})
      })
    },
    // 编辑
    handleEdit (row) {
      this.actionType = 'edit'
      var item = {
        value: '/home/course-data-edit'
      }
      this.$store.commit('common/CLOSE_TAG', item)
      this.$nextTick(() => {
        this.$router.push({path: '/home/course-data-edit', query: {action: 'edit', id: row.id}})
      })
      this.rowData = row
    },
    handleView (row) {
      this.actionType = 'view'
      var item = {
        value: '/home/course-data-edit'
      }
      this.$store.commit('common/CLOSE_TAG', item)
      this.$nextTick(() => {
        this.$router.push({path: '/home/course-data-edit', query: {action: 'view', id: row.id}})
      })
      this.rowData = row
    },
    handleDelete (row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id
        }
        this.$store.dispatch('manage/CourseRemove', params).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.msg
            })
            this.tablePage = 1
            this.handleList(
              this.selectForm.dateData[0],
              this.selectForm.dateData[1],
              this.selectForm.courseType,
              this.selectForm.courseState,
              this.selectForm.teachCoachUserId,
              this.selectForm.key
            )
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
          }
        }).catch((err) => {
          this.$message({type: 'error', message: err.msg})
        })
      }).catch(() => {
      })
    },
    handleComfirm (row) {
      this.$confirm('确定要确认支付吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          signCourseId: row.id
        }
        this.$store.dispatch('manage/ConfirmPayment', params).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.handleList(
            this.selectForm.dateData[0],
            this.selectForm.dateData[1],
            this.selectForm.courseType,
            this.selectForm.courseState,
            this.selectForm.teachCoachUserId,
            this.selectForm.key
          )
        })
      }).catch(() => {
      })
    },
    // 改变pagesize
    handleSizeChange (val) {
      this.page.pageSize = val
      this.handleList(
        this.selectForm.dateData[0],
        this.selectForm.dateData[1],
        this.selectForm.courseType,
        this.selectForm.courseState,
        this.selectForm.teachCoachUserId,
        this.selectForm.key
      )
    },
    // 改变页码
    handleCurrentChange (val) {
      this.tablePage = val
      this.handleList(
        this.selectForm.dateData[0],
        this.selectForm.dateData[1],
        this.selectForm.courseType,
        this.selectForm.courseState,
        this.selectForm.teachCoachUserId,
        this.selectForm.key
      )
    },
    // 表格选择
    handleSelectChange (val) {
      // console.log(val)
      var arr = []
      for (var i = 0; i < val.length; i++) {
        arr.push(val[i].id)
      }
      this.selectIds = arr
      // console.log(this.selectIds)
    },
    // 列表接口
    handleList (payTimeStart, payTimeEnd, courseType, courseState, teachCoachUserId, key) {
      this.tableLoading = true
      var params = {
        'size': this.page.pageSize,
        'current': this.tablePage,
        'payTimeStart': payTimeStart ? payTimeStart : '',
        'payTimeEnd': payTimeEnd ? payTimeEnd : '',
        'signCourseStates': courseState,
        'courseType': courseType,
        'teachCoachUserId': teachCoachUserId,
        'key': key,
        'ext': {},
        'orders': [
          {
            'order': 'desc',
            'sort': 'createTime'
          }
        ]
      }
      this.$store.dispatch('manage/SignPage', params).then(res => {
        // console.log(res)
        if (res.code=='200') {
          var data = res.data.records
          this.tableData = data
          this.page.total = parseInt(res.data.total)
          this.page.currentPage = this.tablePage
        }
        this.tableLoading = false
      }).catch(err => {
        console.log(err)
        this.tableLoading = false
      })
    },
    // 关闭弹窗
    closeDialog (params) {
      if (params == 'edit') {
        this.dialogIndexEdit = false
      }
    },
    // 确认
    confirmDialog (params) {
      if (params == 'edit') {
        this.dialogIndexEdit = false
      }
    },
    handleSearch () {
      this.selectForm.dateData = this.ruleForm.dateData ? this.ruleForm.dateData : []
      this.selectForm.courseType = this.ruleForm.courseType
      this.selectForm.courseState = this.ruleForm.courseState
      this.selectForm.teachCoachUserId = this.ruleForm.teachCoachUserId
      this.selectForm.key = this.ruleForm.key
      this.tablePage = 1
      this.handleList(
        this.selectForm.dateData[0],
        this.selectForm.dateData[1],
        this.selectForm.courseType,
        this.selectForm.courseState,
        this.selectForm.teachCoachUserId,
        this.selectForm.key
      )
    },
    handleExport () {
      this.tableLoading = true
      var params = {
        page: this.tablePage,
        limit: this.page.pageSize,
        id: this.selectId,
        username: this.selectUsername,
        nickname: this.selectNickname
      }
      this.$store.dispatch('manage/ExportUser', params).then(res => {
        var blob = res
        var objectUrl = URL.createObjectURL(blob)
        window.location.href = objectUrl
        this.tableLoading = false
      }).catch(err => {
        console.log(err)
        this.tableLoading = false
      })
    }
  }
}
</script>

<style lang="scss">
.data {
  padding: 0px !important;
  height: auto !important;
  .btn-list {
    float: right;
    margin-right: 10px;
    font-size: 12px;
    color: #409EFF;
    cursor: pointer;
  }
  .el-card__header {
    border-bottom: none;
  }
  .el-card__header,
  .el-card__body {
    padding: 15px;
  }
  .fix-lh {
    line-height: 32px;
  }
  .el-input.is-disabled .el-input__inner {
    color: #606266;
  }
  .left{
    min-height:500px;
    background:#fff;
  }
  .left-tree{
    width: 100%;
    min-height:600px;
    overflow: auto;
  }
  .left-top{
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 16px;
    color: #333;
    background: #e2e2e2;
    font-size: 15px;
  }
}
</style>
